package com.mapfinal.server.layer.web;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.lambkit.common.ResultKit;
import com.lambkit.db.mgr.MgrTable;
import com.lambkit.db.sql.condition.ConditionBuilder;
import com.lambkit.db.sql.condition.SqlBuilder;
import com.mapfinal.server.layer.model.MfWorkspace;
import com.mapfinal.server.web.MapfinalServerController;

public class WorkspaceController extends MapfinalServerController {


	/**
	 * 数据详细页
	 */
	public void index() {
		MgrTable tbc = getTableView(MfWorkspace.service().getTableName(), true);
		String name = getPara(0) == null ? getPara("name") : getPara(0);
		keepTable(tbc, MfWorkspace.service().findById(name));
		render("index.html");
	}	

	/**
	 * 单条数据
	 */
	public void get() {
		getTableView(MfWorkspace.service().getTableName(), true);
		String name = getPara(0) == null ? getPara("name") : getPara(0);
		MfWorkspace datas = MfWorkspace.service().findById(name);
		if (datas == null) {
			renderJson(ResultKit.json(0, "fail", null));
		} else {
			renderJson(ResultKit.json(1, "success", datas));
		}
	}

	/**
	 * 数据列表页
	 */
	public void list() {
		getTableDefault(MfWorkspace.service().getTableName(), true);
		render("list.html");
	}

	/**
	 * 分页
	 */
	public void page() {
		MgrTable tbc = getTableView(MfWorkspace.service().getTableName(), true);
		if (tbc == null) {
			renderJson(ResultKit.json(0, "fail", "table config is null"));
			return;
		}
		ConditionBuilder cb = getConditionsSQL(tbc).build("");
		SqlBuilder sb = new SqlBuilder();
		String select = sb.appendSelect(tbc).build();
		sb.clear().appendFrom(tbc).appendConditions(cb);
		// 排序
		String orderby = getPara("orderby");
		if (StrKit.notBlank(orderby))
			sb.appendOrderBy(orderby);
		String sql = sb.build();
		Integer pNumber = getParaToInt("pageNum", getParaToInt("page", 1));
		Integer pSize = getParaToInt("numPerPage", getParaToInt("limit", 15));
		Page<Record> page = Db.paginate(pNumber, pSize, select, sql, cb.getSqlParas());
		String type = getPara("rt", "default");
		if ("default".equalsIgnoreCase(type)) {
			renderJson(ResultKit.json(1, "success", page));
		} else {
			renderJson(ResultKit.page(type, 1, "success", page.getTotalRow(), page.getList()));
		}

	}

	public void add() {
		render("add.html");
	}

	public void edit() {
		getTableView(MfWorkspace.service().getTableName(), true);
		String name = getPara(0) == null ? getPara("name") : getPara(0);
		setAttr("model", MfWorkspace.service().findById(name));
		render("add.html");
	}

	@Before(MfWorkspaceValidator.class)
	public void save() {
		if (isPOST()) {
			MfWorkspace env = getModel(MfWorkspace.class, "model");
			boolean flag = env.save();
			if (flag) {
				renderJson(ResultKit.json(1, "success", env));
			} else {
				renderJson(ResultKit.json(0, "fail", "失败"));
			}
		}
	}

	@Before(MfWorkspaceValidator.class)
	public void update() {
		if (isPOST()) {
			MfWorkspace env = getModel(MfWorkspace.class, "model");
			boolean flag = env.update();
			if (flag) {
				renderJson(ResultKit.json(1, "success", env));
			} else {
				renderJson(ResultKit.json(0, "fail", "失败"));
			}
		}
	}

	public void delete() {
		if (isPOST()) {
			boolean flag = false;
			String name = getPara(0) == null ? getPara("name") : getPara(0);
			MfWorkspace datas = MfWorkspace.service().findById(name);
			if (datas != null) {
				flag = datas.delete();
			}
			if (flag) {
				renderJson(ResultKit.json(1, "success", "deleted"));
			} else {
				renderJson(ResultKit.json(0, "fail", "deleted"));
			}
		}
	}
}
